package com.vueblog.webapi.config;

import com.vueblog.webapi.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Component;

/**
 * 监听spring容器，执行启动和销毁时执行代码。
 * @author lsj
 * @create 2021-06-10 14:37
 */
@Component
public class SpringEventCheckConfig implements ApplicationListener<ApplicationEvent> {

    @Autowired
    private IUserService userService;

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if(event instanceof ContextClosedEvent){
            userService.setIsOnLine(false);// 设置用户登录状态为false
//            System.out.println(event.getClass().getSimpleName()+" 关闭事件已发生！");
        }else if(event instanceof ContextRefreshedEvent){
//            System.out.println(event.getClass().getSimpleName()+" 跟新事件已发生！");
        }else if(event instanceof ContextStartedEvent){
//            System.out.println(event.getClass().getSimpleName()+" 开始事件已发生！");
        }else if(event instanceof ContextStoppedEvent){
//            System.out.println(event.getClass().getSimpleName()+" 停止事件已发生！");
        }else{
//            System.out.println("有其它事件发生:"+event.getClass().getName());
        }
    }
}
